﻿// -------------------------------------------------------------------------
//    @FileName         :    NFFashionPart.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-30
//    @Email            :    445267987@qq.com
//    @Module           :    NFFashionPart
//
// -------------------------------------------------------------------------

#include "NFFashionPart.h"

#include <NFFashionUtil.h>

#include "NFPlayer.h"
#include "proto_db.pb.h"

NFFashionPart::NFFashionPart()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFFashionPart::~NFFashionPart()
{
}

int NFFashionPart::CreateInit()
{
    return 0;
}

int NFFashionPart::ResumeInit()
{
    return 0;
}

int NFFashionPart::LoadFromDB(const proto_ff::tbRoleDetail* pDBData)
{
    m_stData.FromPb(pDBData->role_fortune_data().fashion_data());
    return 0;
}

int NFFashionPart::InitConfig(proto_ff::tbRoleDetail* pDbData, const RoleOne& stRoleOne)
{
    CHECK_NULL(0, pDbData);
    auto pRoleFashionData = pDbData->mutable_role_fortune_data()->mutable_fashion_data();
    CHECK_NULL(0, pRoleFashionData);

    pRoleFashionData->set_is_change(false);
    pRoleFashionData->set_attr_slot(proto_ff::EN_FASHION_SLOT_TYPE_ONE);
    pRoleFashionData->set_show_slot(proto_ff::EN_FASHION_SLOT_TYPE_ONE);
    for (int j = 1; j < proto_ff::enFashionType_ARRAYSIZE; ++j)
    {
        proto_ff::FashionOne* fashionOne = pRoleFashionData->add_dressed_list();
        proto_ff::FashionOne* fashionOne_2 = pRoleFashionData->add_dressed_list_2();

        fashionOne->set_id(0);
        fashionOne->set_time_limit(0);
        fashionOne->set_uuid(0);

        fashionOne_2->set_id(0);
        fashionOne_2->set_time_limit(0);
        fashionOne_2->set_uuid(0);
    }
    return 0;
}

int NFFashionPart::SaveDB(proto_ff::tbRoleDetail* pDBData)
{
    m_stData.ToPb(pDBData->mutable_role_fortune_data()->mutable_fashion_data());
    return 0;
}

int NFFashionPart::SaveRoleSimpleInfo(proto_ff::Proto_RoleSimpleInfo* pDbData)
{
    CHECK_NULL(0, pDbData);
    int fashionSize = GetSlotSize(GetShowSlot());
    for (int i = 0; i < fashionSize; ++i)
    {
        FashionOne* tmpOne = GetSlot((proto_ff::enFashionType)(i + 1), GetShowSlot());
        if (!tmpOne)
            continue;

        int iFashionID = tmpOne->id;
        if (iFashionID == 0)
        {
            iFashionID = NFFashionUtil::GetEquipFashionID(m_roleId, i);
        }

        pDbData->add_fashions(iFashionID);
    }

    return 0;
}

int NFFashionPart::MakePlayerDataCS(proto_ff::Proto_CSGetRoleRsp* pCSGetRoleRsp)
{
    m_stData.ToPb(pCSGetRoleRsp->mutable_fashion_data());
    return 0;
}

int NFFashionPart::MakeCityPlayerData(proto_ff::CityRoleInfo* pGCCityRoleInfo)
{
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);
    CHECK_NULL(0, pGCCityRoleInfo);
    int fashionSize = GetSlotSize(GetShowSlot());
    for (int i = 0; i < fashionSize; ++i)
    {
        FashionOne* tmpOne = GetSlot((proto_ff::enFashionType)(i + 1), GetShowSlot());
        if (!tmpOne)
            continue;

        int iFashionID = tmpOne->id;
        if (iFashionID == 0)
        {
            iFashionID = NFFashionUtil::GetEquipFashionID(pPlayer->GetRoleID(), i);
        }

        pGCCityRoleInfo->add_fashions(iFashionID);
    }
    return 0;
}

proto_ff::FashionSlotType NFFashionPart::GetAttrSlot()
{
    if (!proto_ff::FashionSlotType_IsValid(m_stData.attr_slot))
    {
        LOGSVR_ERROR("slot err:" << m_roleId << " " << m_stData.attr_slot << " " << m_stData.dressed_list_2.size());
    }

    return static_cast<proto_ff::FashionSlotType>(m_stData.attr_slot);
}

proto_ff::FashionSlotType NFFashionPart::GetShowSlot()
{
    if (!proto_ff::FashionSlotType_IsValid(m_stData.show_slot))
    {
        LOGSVR_ERROR("slot err:" << m_roleId << " " << m_stData.show_slot << " " << m_stData.dressed_list_2.size());
    }

    return static_cast<proto_ff::FashionSlotType>(m_stData.show_slot);
}

int NFFashionPart::GetSlotSize(proto_ff::FashionSlotType type)
{
    return GetSlotSize(type, &m_stData);
}

int NFFashionPart::GetSlotSize(proto_ff::FashionSlotType type, RoleFashionData* stData)
{
    if (type == proto_ff::EN_FASHION_SLOT_TYPE_ONE)
    {
        if (stData->dressed_list.size() > stData->dressed_list.max_size())
        {
            LOGSVR_ERROR("size err:" << stData->dressed_list.size());
            stData->dressed_list.resize(stData->dressed_list.max_size());
        }

        return stData->dressed_list.size();
    }
    else if (type == proto_ff::EN_FASHION_SLOT_TYPE_TWO)
    {
        if (stData->dressed_list_2.size() > stData->dressed_list_2.max_size())
        {
            LOGSVR_ERROR("size err:" << stData->dressed_list_2.size());
            stData->dressed_list_2.resize(stData->dressed_list_2.max_size());
        }

        return stData->dressed_list_2.size();
    }

    return 0;
}

int NFFashionPart::GetSpawnSize(proto_ff::FashionSlotType type)
{
    return GetSpawnSize(type, &m_stData);
}

int NFFashionPart::GetSpawnSize(proto_ff::FashionSlotType type, RoleFashionData* stData)
{
    if (type == proto_ff::EN_FASHION_SLOT_TYPE_ONE)
    {
        if (stData->spawn_list.size() > stData->spawn_list.max_size())
        {
            LOGSVR_ERROR("size err:" << stData->spawn_list.size());
            stData->spawn_list.resize(stData->spawn_list.max_size());
        }

        return stData->spawn_list.size();
    }
    else if (type == proto_ff::EN_FASHION_SLOT_TYPE_TWO)
    {
        if (stData->spawn_list_2.size() > stData->spawn_list_2.max_size())
        {
            LOGSVR_ERROR("size err:" << stData->spawn_list_2.size());
            stData->spawn_list_2.resize(stData->spawn_list_2.max_size());
        }

        return stData->spawn_list_2.size();
    }

    return 0;
}

void NFFashionPart::SetTransfer(bool isChange)
{
    m_stData.is_change = isChange;
    MarkDirty();
}

void NFFashionPart::SetAttrSlot(proto_ff::FashionSlotType type)
{
    m_stData.attr_slot = static_cast<FashionSlotType>(type);
    MarkDirty();
}

void NFFashionPart::SetShowSlot(proto_ff::FashionSlotType type)
{
    m_stData.show_slot = static_cast<FashionSlotType>(type);
    MarkDirty();
}


FashionOne* NFFashionPart::GetSlot(proto_ff::enFashionType type, proto_ff::FashionSlotType slot)
{
    return GetSlot(type, slot, &m_stData);
}

FashionOne* NFFashionPart::GetSlot(proto_ff::enFashionType type, proto_ff::FashionSlotType slot, RoleFashionData* stData)
{
    if (type == proto_ff::EN_FASHION_TYPE_NONE || !enFashionType_IsValid(type))
        return nullptr;

    if (!FashionSlotType_IsValid(slot))
        return nullptr;

    return GetSlotByIndex(slot, stData, type - 1);
}

FashionOne* NFFashionPart::GetSlotByIndex(proto_ff::FashionSlotType slot, int iIndex)
{
    return GetSlotByIndex(slot, &m_stData, iIndex);
}

FashionOne* NFFashionPart::GetSlotByIndex(proto_ff::FashionSlotType slot, RoleFashionData* stData, int iIndex)
{
    if (slot == proto_ff::EN_FASHION_SLOT_TYPE_ONE)
    {
        if (iIndex >= 0 && iIndex < static_cast<int>(stData->dressed_list.size()))
        {
            return &stData->dressed_list[iIndex];
        }
    }
    else if (slot == proto_ff::EN_FASHION_SLOT_TYPE_TWO)
    {
        if (iIndex >= 0 && iIndex < static_cast<int>(stData->dressed_list_2.size()))
        {
            return &stData->dressed_list_2[iIndex];
        }
    }
    else
    {
        LOGSVR_ERROR("slot err:" << slot);
    }

    return nullptr;
}

SpawnFashionID* NFFashionPart::GetSpawn(int index, proto_ff::FashionSlotType slot, RoleFashionData* stData)
{
    int spawnSize = GetSpawnSize(slot, stData);
    if (index < 0 || index >= spawnSize)
        return nullptr;

    return GetSpawnByIndex(slot, stData, index);
}

SpawnFashionID* NFFashionPart::GetSpawnByIndex(proto_ff::FashionSlotType slot, RoleFashionData* stData, int iIndex)
{
    if (slot == proto_ff::EN_FASHION_SLOT_TYPE_ONE)
    {
        if (iIndex >= 0 && iIndex < static_cast<int>(stData->spawn_list.size()))
        {
            return &stData->spawn_list[iIndex];
        }
    }
    else if (slot == proto_ff::EN_FASHION_SLOT_TYPE_TWO)
    {
        if (iIndex >= 0 && iIndex < static_cast<int>(stData->spawn_list_2.size()))
        {
            return &stData->spawn_list_2[iIndex];
        }
    }
    else
    {
        LOGSVR_ERROR("slot err:" << slot);
    }

    return nullptr;
}

SpawnFashionID* NFFashionPart::GetSpawnByIndex(proto_ff::FashionSlotType slot, int iIndex)
{
    return GetSpawnByIndex(slot, &m_stData, iIndex);
}
